var OpenID='-996';   //微信号的Openid
var portID;
var barCode;  //细码单单号
var LastFHitemid=0;  //当前选择发货的最后一笔数量的主键
var FHbillID;  //发货草稿主表  主键
var JudgeMa=1;  //用来判断当前是分码还是并码  1表示分  2表示并


$(function () {

    portID=7072;

    $('#A').window('close');

    // $('.datagrid-cell').css('font-size','36px');

    getlastFHQty(1);  //获取最后一笔发货数据 参数是用来判断是否是第一次打开软件
    getlastFHQtySum();          ////获取已发数量 修改按钮上的发货数量显示
    GetintoCaoDetail();
    getsum02();

});

//根据细码单号获取对应的打卷数和计划信息
function getPlanDetail() {
    // var datacole=[];
    // $('#detai01').datagrid('loadData', datacole);
    console.log('http://182.254.216.227:'+ portID +'/white/ds/query/'
        + "call usp_getJRKDetail('" + barCode + "')");
    $.ajax({
        url: 'http://182.254.216.227:'+ portID +'/white/ds/query/'
            + "call usp_getJRKDetail('" + barCode + "')",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.length>0) {
                $('#dh').val(data[0].b_barcode);
                $('#kh').val(data[0].b_clientname);
                $('#mf').val(data[0].b_firstgg);
                $('#pm').val(data[0].b_pinming);
            }
            $('#detai01').datagrid('loadData', data);
            console.log(data);

        }, complete: function (XMLHttpRequest, textStatus) {
            console.log("进入到complete");
        }

    })
}

//确定选择发货
function choiceFH() {
    var currentRow = $("#detai01").datagrid("getSelected");//获得选中行的信息
    if (currentRow!=undefined){

        var JRKItemid=currentRow.B_ItemID;
        var JHDid=currentRow.B_ID;
        var GJ=currentRow.B_GJ;
        var AorB=currentRow.B_AorB;
        var ylgg=currentRow.b_ylgg;
        var kh=$('#kh').val().toString();
        var pm=$('#pm').val().toString();
        var mf=$('#mf').val().toString();

        // var strSQL='INSERT into Transientdraft (itemid,Qty,AorB,planID,barcode,clientname,pinming,menfu,openid,ylgg)'
        //           + "values('" + JRKItemid +"','" + GJ +"','"+ AorB +"','"+ JHDid +"','"
        //     + barCode +"','"+ kh +"','"+ pm +"','"+ mf +"','"+ OpenID +"','"+ ylgg +"')";
        // console.log(strSQL);
        // $.ajax({
        //      url: 'http://182.254.216.227:'+ portID +'/white/ds/exec',
        //      // url: 'http://www.xdfznh.club:'+ portID +'/white/ds/exec',
        //     type: 'POST',
        //     dataType: 'json',
        //     data: JSON.stringify({'sql':strSQL}),
        //     contentType: "application/json;charset=UTF-8",
        //     success: function (data) {
        //         UpdateJRK(JRKItemid);
        //         // getPlanDetail(barCode);
        //     }
        // });
        console.log('确定选择发货');
        $.ajax({
            url: 'http://182.254.216.227:'+ portID +'/white/fh/set/' + JRKItemid + "/" + OpenID,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                if (data.data) {
                    //第一次打开软件时如果有选择了发货   但未选择上车的数据  自动显示出来
                    LastFHitemid = data.data.id;
                    barCode=data.data.barcode;
                    LastStr="已发末匹：" + data.data.qty + " / " + data.data.aorB;
                    $('#close').linkbutton({text:LastStr});
                    getPlanDetail(barCode);
                    getsum();
                }else{
                    LastFHitemid = 0;
                    LastStr="已发末匹：无" ;
                    $('#close').linkbutton({text:LastStr});
                }
                console.log(data);

            }, complete: function (XMLHttpRequest, textStatus) {
                console.log("进入到complete");
            }

        })

    }else{
        $.messager.alert('确认','当前没有选中需要发货数据!');
    }

}

//确定公斤发货之后  修改 JRKbill 标记，并重新获取数据
function UpdateJRK(JRKitemid) {
    var strSQL="update g_jrkbill set B_FPDID='-99' where B_ItemID="+ JRKitemid;
    console.log(strSQL);
    $.ajax({
        url: 'http://182.254.216.227:'+ portID +'/white/ds/exec',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({'sql':strSQL}),
        contentType: "application/json;charset=UTF-8",
        success: function (data) {
            getlastFHQty(0);
            getlastFHQtySum();
            getPlanDetail(barCode);
            getsum();
        }
    });

}

//获取最后一笔发货数据 参数是用来判断是否是第一次打开软件
function getlastFHQty(firstOpen) {
    var LastStr;

    console.log('http://182.254.216.227:'+ portID +'/white/ds/query/'
        + "select * from Transientdraft where openid='"+ OpenID + "' ORDER BY ID DESC");

    $.ajax({
        url: 'http://182.254.216.227:'+ portID +'/white/ds/query/'
            + "select * from Transientdraft where openid='"+ OpenID + "' ORDER BY ID DESC",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.length>0) {
                if(firstOpen>0){
                    //第一次打开软件时如果有选择了发货   但未选择上车的数据  自动显示出来
                    LastFHitemid = data[0].ID;
                    barCode=data[0].barcode;
                    LastStr="已发末匹：" + data[0].Qty + " / " + data[0].AorB;
                    $('#close').linkbutton({text:LastStr});
                    getPlanDetail(barCode);
                    getsum();
                }else{
                    LastFHitemid = data[0].ID;
                    LastStr="已发末匹：" + data[0].Qty + " / " + data[0].AorB;
                    $('#close').linkbutton({text:LastStr});
                }
            }else{
                LastFHitemid = 0;
                LastStr="已发末匹：无" ;
                $('#close').linkbutton({text:LastStr});
            }
        }, complete: function (XMLHttpRequest, textStatus) {
            // console.log("进入到complete");
        }

    })

}

//获取已发数量 修改按钮上的发货数量显示
function getlastFHQtySum() {
    console.log('http://182.254.216.227:'+ portID +'/white/ds/query/'
        + "select ifnull(sum(Qty),0)as Qty,count(barcode)as PS from Transientdraft where openid='"+ OpenID + "'");

    $.ajax({
        url: 'http://182.254.216.227:'+ portID +'/white/ds/query/'
            + "select ifnull(sum(Qty),0)as Qty,count(barcode)as PS from Transientdraft where openid='"+ OpenID + "'",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.length>0) {
                var LastStr="已发匹数：" + data[0].PS + "，已发重量： " + data[0].Qty;
                $('#sumNumber').linkbutton({text:LastStr});

            }
        }, complete: function (XMLHttpRequest, textStatus) {
            // console.log("进入到complete");
        }

    })

}

//删除最后一匹数据
function deletelastP() {
    var LastStr;
    if (LastFHitemid==0){
        $.messager.alert('确认','当前没有已发货数据!');
        return;
    }

    $.messager.defaults = {ok: "确定", cancel: "取消", width: 250, closable: false};
    $.messager.confirm('确认', '您确认要删除最后一匹发货数据吗？ ', function (r) {
        if (r) {
            console.log('确定删除最后一行数据');
            console.log('http://182.254.216.227:'+ portID +'/white/fh/del/'+ LastFHitemid + '/' + OpenID);
            $.ajax({
                url: 'http://182.254.216.227:'+ portID +'/white/fh/del/'+ LastFHitemid + '/' + OpenID,
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    if (data.data) {
                        console.log("进入success事件")
                        //第一次打开软件时如果有选择了发货   但未选择上车的数据  自动显示出来
                        LastFHitemid = data.data.id;
                        barCode=data.data.barcode;
                        LastStr="已发末匹：" + data.data.qty + " / " + data.data.aorB;
                        $('#close').linkbutton({text:LastStr});
                        getPlanDetail(barCode);
                        getsum();
                    }else{
                        LastFHitemid = 0;
                        LastStr="已发末匹：无" ;
                        $('#close').linkbutton({text:LastStr});
                    }
                    console.log(data);

                }, complete: function (XMLHttpRequest, textStatus) {
                    console.log("进入到complete");
                }

            })

            // var strSQL="delete from Transientdraft where Itemid="+ LastFHitemid;
            // console.log(strSQL);
            // $.ajax({
            //     url: 'http://182.254.216.227:'+ portID +'/white/ds/exec',
            //     type: 'POST',
            //     dataType: 'json',
            //     data: JSON.stringify({'sql':strSQL}),
            //     contentType: "application/json;charset=UTF-8",
            //     success: function (data) {
            //         UpdateJRKFH();
            //         getlastFHQty(0);
            //         getlastFHQtySum();
            //         getPlanDetail(barCode);
            //         getsum();
            //     }
            // });


        } else {
            console.log('取消');
        }
    });

}

//删除临时表数据之后   修改打卷数据的发货标记
function UpdateJRKFH() {
    var strSQL="update g_jrkbill set B_FPDID=0 where b_itemid="+ LastFHitemid;
    console.log(strSQL);
    $.ajax({
        url: 'http://182.254.216.227:'+ portID +'/white/ds/exec',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({'sql':strSQL}),
        contentType: "application/json;charset=UTF-8",
        success: function (data) {
            console.log('恢复打卷发货标记完成！');
        }
    });
}

//上车
function getintocar() {
    if (LastFHitemid>0){
        $.messager.defaults = {ok: "确定", cancel: "取消", width: 250, closable: false};
        $.messager.confirm('确认', '确定选择上车？ ', function (r) {
            if (r) {
                $.ajax({
                    url: 'http://182.254.216.227:' + portID + '/white/ds/query/'
                        + "call usp_GetIntoCar('" + barCode + "','" + OpenID + "','"+ FHbillID + "')",
                    type: 'get',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data[0].id);
                        FHbillID=data[0].id;
                        getlastFHQty(0);
                        getlastFHQtySum();
                        GetintoCaoDetail();
                        getsum02();
                        get
                    }, complete: function (XMLHttpRequest, textStatus) {
                        // console.log("进入到complete");
                    }

                })
            }
        })

    }else{
        $.messager.alert('确认','当前没有已确定发货数据!');
        return;
    }

}

//整单发货上车
function getintocarAll() {
    $.messager.defaults = {ok: "确定", cancel: "取消", width: 250, closable: false};
    $.messager.confirm('确认', '确定整单选择发货上车？ ', function (r) {
        if (r) {
            $.ajax({
                url: 'http://182.254.216.227:' + portID + '/white/ds/query/'
                    + "call usp_GetIntoCarAll('" + barCode + "','" + OpenID + "','"+ FHbillID + "')",
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    console.log(data[0].id);
                    FHbillID=data[0].id;
                    getlastFHQty(0);
                    getlastFHQtySum();
                    GetintoCaoDetail();
                    getsum02();
                    getPlanDetail(barCode);
                    getsum();
                }, complete: function (XMLHttpRequest, textStatus) {}

            })
        }
    })
}

//获取已发汇总的数据   区分 分码和并码
function GetintoCaoDetail() {
    $.ajax({
        url: 'http://182.254.216.227:' + portID + '/white/ds/query/'
            + "call usp_GetIntoCarDetail('" + OpenID + "','" + JudgeMa + "')",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data[0].id);
            FHbillID=data[0].ID;
            $('#detai02').datagrid('loadData', data);
            console.log("草稿表主键：" + FHbillID)
        }, complete: function (XMLHttpRequest, textStatus) {
            // console.log("进入到complete");
        }

    })
}

//获取发货单据编号
function getBarCodeFH() {



}

//************************************************************************//

function  getsum() {
    //扫码发货页面网格数据汇总
    $('#detai01').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps=0;

            for (let i = 0; i < data.length; i++) {
                qty += data[i].B_GJ ? parseFloat(data[i].B_GJ) : 0;
                ps=ps+1;
            }
            console.log(qty);
            $(this).datagrid('reloadFooter', [{
                // client_name: '合计',
                B_GJ: qty.toFixed(1),
                B_AorB: ps,
            }])
        }
    });


}

function  getsum02() {
    //扫码发货页面网格数据汇总
    $('#detai02').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps=0;

            for (let i = 0; i < data.length; i++) {
                qty += data[i].Qty ? parseFloat(data[i].Qty) : 0;
                ps += data[i].PS ? parseFloat(data[i].PS) : 0;
            }
            console.log(qty);
            $(this).datagrid('reloadFooter', [{
                PinMing: '合计',
                Qty: qty.toFixed(1),
                PS: ps,
            }])
        }
    });


}

//************************************************************************//
//录入细码单号
function InputbarCode() {
    if (LastFHitemid>0){
        $.messager.alert('确认','当前已经有发货数据未上车，不允许更细码单!');
        return;
    }
    $('#A').window('open');
}

//录入数值 小键盘数字拼接
function getnumberStr(data) {
    var NewStr;
    NewStr=$('#XMbarcodetext').val().toString();  //先获取当前文本框中的值

    if (NewStr.length>0) {
        NewStr = NewStr + data
    }
    else{
        NewStr=data
    }

    $('#XMbarcodetext').val(NewStr);
}

//录入数值  取消
function cancel01() {
    $('#XMbarcodetext').val('');
    $('#A').window('close');
}

//录入数值 确定
function determine01() {

    $('#dh').val($('#XMbarcodetext').val().toString());
    barCode=$('#XMbarcodetext').val().toString();
    $('#XMbarcodetext').val('');
    $('#A').window('close');

    if (barCode.length>0) {
        getPlanDetail(barCode);
        getsum();
    }
}


